package com.itheima.manager;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ArrayUtil;
import com.itheima.domain.SmsCoupon;
import com.itheima.domain.SmsCouponProductCategoryRelation;
import com.itheima.domain.SmsCouponProductRelation;
import com.itheima.service.db.SmsCouponProductCategoryRelationService;
import com.itheima.service.db.SmsCouponProductRelationService;
import com.itheima.service.db.SmsCouponService;
import com.itheima.util.R;
import com.itheima.vo.SmsCouponVo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class SmsCouponManager {

    @DubboReference
    SmsCouponService smsCouponService;

    @DubboReference
    SmsCouponProductCategoryRelationService smsCouponProductCategoryRelationService;

    @DubboReference
    SmsCouponProductRelationService smsCouponProductRelationService;

    public R findByPage(Integer pageNum, Integer pageSize,String name,Integer type) {
        return R.success(smsCouponService.findByPage(pageNum, pageSize,name,type));
    }

    public R findById(Long couponId) {
        //根据couponId查询SmsCoupon表
        SmsCoupon smsCoupon = smsCouponService.findById(couponId);

        //根据couponId查询SmsCouponProductCategoryRelation表
        List<SmsCouponProductCategoryRelation> productCategoryRelationList = smsCouponProductCategoryRelationService.findByCouponId(couponId);

        //根据couponId查询SmsCouponProductRelation表
        List<SmsCouponProductRelation> productRelationList = smsCouponProductRelationService.findByCouponId(couponId);

        //封装数据
        SmsCouponVo vo = new SmsCouponVo();
        BeanUtil.copyProperties(smsCoupon,vo);
        vo.setProductRelationList(productRelationList);
        vo.setProductCategoryRelationList(productCategoryRelationList);

        return R.success(vo);
    }

    public R updateById(SmsCoupon smsCoupon) {
        smsCouponService.updateById(smsCoupon);
        return R.success(null);
    }

    public R deleteById(Long id) {
        smsCouponService.deleteById(id);
        return R.success(null);
    }

    public R save(SmsCouponVo smsCouponVo) {
        SmsCoupon smsCoupon = new SmsCoupon();
        BeanUtil.copyProperties(smsCouponVo,smsCoupon);
        smsCouponService.save(smsCoupon);

        List<SmsCouponProductCategoryRelation> productCategoryRelationList = smsCouponVo.getProductCategoryRelationList();
        if (CollUtil.isNotEmpty(productCategoryRelationList)) {
            for (SmsCouponProductCategoryRelation smsCouponProductCategoryRelation : productCategoryRelationList) {
                smsCouponProductCategoryRelation.setCouponId(smsCoupon.getId());
                smsCouponProductCategoryRelationService.save(smsCouponProductCategoryRelation);
            }
        }

        List<SmsCouponProductRelation> productRelationList = smsCouponVo.getProductRelationList();
        if (CollUtil.isNotEmpty(productRelationList)) {
            for (SmsCouponProductRelation smsCouponProductRelation : productRelationList) {
                smsCouponProductRelation.setCouponId(smsCoupon.getId());
                smsCouponProductRelationService.save(smsCouponProductRelation);
            }
        }

        return R.success(null);
    }
}
